#include "../macro.h"
#include <iostream>
#include <unistd.h>
#include <sys/shm.h>
#include <string.h>
#include <vector>

struct Buf{
    int useful;
    char text[1024];
};

void read()
{
    int shmid = shmget(1000, sizeof(struct Buf), IPC_CREAT | 0600);
    ERR_CHECK(shmid, -1, "shmget")

    struct Buf *buf = (struct Buf *)shmat(shmid, NULL, 0);
    ERR_CHECK(buf, (struct Buf *)-1, "shmat")
    while(1){
        if(buf->useful == 1){
            ::write(STDOUT_FILENO, buf->text, strlen(buf->text));
            buf->useful = 0;
            if(strncmp(buf->text, "end", 3) == 0){
                break;
            }
        }
    }

    shmdt(buf);
    shmctl(shmid, IPC_RMID, NULL);
    printf("read close\n");
}

int main(int argc, char const *argv[])
{
    /* code */
    read();
    return 0;
}
